package net.huashitong.supervision.task.service;

import com.sirdc.modules.sys.dao.SysUserDao;
import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.entity.SysLogin;
import com.sirdc.modules.sys.filter.SysUserFilter;
import com.sirdc.modules.sys.service.SysDeptService;
import com.sirdc.modules.sys.service.SysLoginService;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.task.entity.TaskMain;
import net.huashitong.supervision.task.filter.TaskMainFilter;
import net.huashitong.util.UtilsTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 攻坚任务 事务管理
 */
@Service
public class TaskCrucialService {
    @Autowired
    private TaskMainService taskMainService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysLoginService sysLoginService;
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SMSUtilService smsUtilService;
    public void addTaskCrucial(TaskMainFilter filter) {
        String inspectionId = filter.getInspectionId();
        if (inspectionId.contains(",")) {
            if (inspectionId.endsWith(",")) {
                inspectionId = inspectionId.substring(0, inspectionId.length() - 1);
            }
            String[] ids = inspectionId.split(",");
            List<TaskMain> taskMainListForAdd = new ArrayList<>();
            for (String id : ids) {
                TaskMain taskMain = new TaskMain();
                UtilsTools.copyPro(filter, taskMain);
                taskMain.setCrucialUser(SysUserUtils.getUserId());
                taskMain.setInspectionId(id);
                taskMain.setStatus("9");
                //为了确保安全
                taskMain.setOverdueDept(null);
                taskMainListForAdd.add(taskMain);
                SysDept dept = sysDeptService.getById(id);
                 //发送短信。查询当前单位的领导
                        SysUserFilter userFilter = new SysUserFilter();
                        userFilter.setRoleId("02");
                        userFilter.setDeptId(dept.getSysId());
                        List<SysLogin> loginList = sysLoginService.queryUserLoginByRoleDept(userFilter);
                        for (SysLogin sysLogin : loginList) {
                            String mobile = sysUserDao.getById(sysLogin.getSysId()).getMobile();
                            if (StringUtils.isNotBlank(mobile)) {
                                smsUtilService.taskMainSms(taskMain,sysUserDao.getById(sysLogin.getSysId()));
                            }
                        }
            }
            taskMainService.batchSave(taskMainListForAdd);
        } else {
            TaskMain taskMain = new TaskMain();
            UtilsTools.copyPro(filter, taskMain);
            taskMain.setCrucialUser(SysUserUtils.getUserId());
            //为了确保安全
            taskMain.setOverdueDept(null);
            taskMain.setStatus("9");
            taskMainService.save(taskMain);
        }
    }

    public void addTaskInspection(TaskMainFilter filter) {
        String overdueDept = filter.getOverdueDept();
        if (overdueDept.contains(",")) {
            if (overdueDept.endsWith(",")) {
                overdueDept = overdueDept.substring(0, overdueDept.length() - 1);
            }
            String[] ids = overdueDept.split(",");
            List<TaskMain> taskMainListForAdd = new ArrayList<>();
            for (String id : ids) {
                TaskMain taskMain = new TaskMain();
                UtilsTools.copyPro(filter, taskMain);
                taskMain.setOverdueDept(id);
                taskMain.setStatus("8");
                //为了确保安全
                taskMain.setCrucialUser(null);
                taskMainListForAdd.add(taskMain);
            }
            taskMainService.batchSave(taskMainListForAdd);
        } else {
            TaskMain taskMain = new TaskMain();
            UtilsTools.copyPro(filter, taskMain);
            //督查组单位id 自动填入  为了确保安全
            taskMain.setCrucialUser(null);
            taskMain.setStatus("8");
            taskMainService.save(taskMain);
        }
    }

}
